import 'package:app/theme/design_tokens.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Modal {
  /// single dialog(TODO)
  static single(BuildContext context,
      {String? title,
      String? content,
      Widget? widget,
      Function? onConfirm,
      Function? onCancel}) {
    Widget? _title = title != null ? Text(title) : null;
    Widget? _content = content != null ? Text(content) : null;
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: Text('选择颜色'),
          children: [
            SimpleDialogOption(
              onPressed: () {
                // 选择红色
                Navigator.of(context).pop('红色');
              },
              child: Text('红色'),
            ),
            SimpleDialogOption(
              onPressed: () {
                // 选择蓝色
                Navigator.of(context).pop('蓝色');
              },
              child: Text('蓝色'),
            ),
          ],
        );
      },
    );
  }

  /// comfirm dialog
  static confirm(BuildContext context,
      {String? title,
      String? content,
      Function? onConfirm,
      Function? onCancel}) {
    Widget? _title = title != null ? Text(title) : null;
    Widget? _content = content != null ? Text(content) : null;
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: _title,
          content: _content,
          actions: <Widget>[
            TextButton(
              onPressed: () {
                // 执行取消操作
                if (onCancel != null) {
                  onCancel();
                }
                Navigator.of(context).pop();
              },
              child: Text('取消'),
            ),
            TextButton(
              onPressed: () {
                // 执行确认操作
                if (onConfirm != null) {
                  onConfirm();
                }
                Navigator.of(context).pop();
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

  /// confirm dialog (ios style)
  static confirmIos(BuildContext context,
      {String? title,
      String? content,
      Function? onConfirm,
      Function? onCancel}) {
    Widget? _title = title != null ? Text(title) : null;
    Widget? _content = content != null ? Text(content) : null;
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: _title,
          content: _content,
          actions: <Widget>[
            CupertinoDialogAction(
              onPressed: () {
                // 执行取消操作
                if (onCancel != null) {
                  onCancel();
                }
                Navigator.of(context).pop();
              },
              child: Text('取消'),
            ),
            CupertinoDialogAction(
              onPressed: () {
                // 执行确认操作
                if (onConfirm != null) {
                  onConfirm();
                }
                Navigator.of(context).pop();
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

  /// bottom sheet dialog (已实现)
  static bottomSheet(BuildContext context, {required Widget child}) {
    showModalBottomSheet(
      isScrollControlled:
          true, //此时弹窗会覆盖更大的屏幕区域，并且允许内容滚动。不过，请注意，这并不会直接设置弹窗的具体高度，而是允许弹窗扩展到更大的范围。
      clipBehavior: Clip.hardEdge, // 裁剪弹窗的边缘
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(DesignTokens.radius3),
              topRight: Radius.circular(DesignTokens.radius3))),
      context: context,
      enableDrag: false,
      builder: (BuildContext context) {
        return child;
      },
    );
  }

  /// bottom sheet dialog (ios style) (TODO)
  static bottomSheetIos(
    BuildContext context,
  ) {
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) {
        return CupertinoActionSheet(
          title: const Text('选择一个选项'),
          message: const Text('请做出你的选择'),
          actions: <CupertinoActionSheetAction>[
            CupertinoActionSheetAction(
              child: const Text('选项一'),
              onPressed: () {
                Navigator.pop(context);
                // 处理选项一的逻辑
              },
            ),
            CupertinoActionSheetAction(
              child: const Text('选项二'),
              onPressed: () {
                Navigator.pop(context);
                // 处理选项二的逻辑
              },
            ),
          ],
          cancelButton: CupertinoActionSheetAction(
            isDestructiveAction: true,
            child: const Text('取消'),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        );
      },
    );
  }
}
